package cn.ultrarisk.scadmin.manager.biz.common.service;

import cn.ultrarisk.mybatis.extension.query.Updater;
import cn.ultrarisk.scadmin.manager.biz.common.context.OperationType;
import cn.ultrarisk.scadmin.manager.biz.common.dao.DictDAO;
import cn.ultrarisk.scadmin.manager.biz.common.entity.Dict;
import cn.ultrarisk.scadmin.manager.biz.common.valid.DictValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 数据字典数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class DictService {
    @Autowired
    private DictDAO dictDAO;
    @Autowired
    private DictValidator validator;
    @Autowired
    private OperationLogService operationLogService;

    public void create(String name, String type, String remark, String value, Integer level) {
        validator.validName(name, true, null);
        validator.validType(type);
        validator.validLevel(level);
        validator.validValue(type, value);

        Dict dict = new Dict();
        dict.setName(name);
        dict.setType(type);
        dict.setRemark(remark);
        dict.setValue(value);
        dict.setLevel(level);
        dictDAO.create(dict);
        operationLogService.createDictLog(OperationType.DICT_CREATE, dict.getId());
    }

    public void update(Integer id, String value) {
        Dict dict = dictDAO.get(id);
        validator.validValue(dict.getType(), value);

        dictDAO.update(id, Updater.update(Dict.VALUE, value));
        operationLogService.createDictLog(OperationType.DICT_UPDATE, id);
    }

    public void delete(Integer id) {
        validator.validDelete(id);
        operationLogService.createDictLog(OperationType.DICT_DELETE, id);
        dictDAO.delete(id);
    }
}